package com.itfreer.ui.audio.recoder;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itfreer.ui.R;

/**
 * 录音按钮
 */
public class RecoderButton extends com.itfreer.ui.base.TextView {

    private PopupWindowFactory mPop;
    private ImageView mImageView;
    private TextView mTextView;
    private AudioRecoderUtils mAudioRecoderUtils;
    private Context context;

    private AudioRecoderUtils.OnAudioStatusUpdateListener listener;

    public void setListener(AudioRecoderUtils.OnAudioStatusUpdateListener listener) {
        this.listener = listener;
    }

    public RecoderButton(Context context) {
        super(context);
        setView(context);
    }

    public RecoderButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setView(context);
    }

    public RecoderButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setView(context);
    }

    private void setView(Context context) {
        this.context = context;
        setTextColor(Color.parseColor("#1eaaf2"));
        setTextSize(40);
        setText(R.string.ly);
        setGravity(TEXT_ALIGNMENT_CENTER);
        setFontName("ui.ttf");
    }

    /**
     * 初始化
     * @param content
     */
    public void init(final View content) {
        this.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        mPop.showAtLocation(content, Gravity.CENTER, 0, 0);
                        setTextColor(Color.GREEN);
                        mAudioRecoderUtils.startRecord();
                        break;
                    case MotionEvent.ACTION_UP:
                        mAudioRecoderUtils.stopRecord();        //结束录音（保存录音文件）
                        mPop.dismiss();
                        setTextColor(Color.parseColor("#1eaaf2"));
                        break;
                }
                return true;
            }
        });

        // PopupWindow的布局文件
        final View view = View.inflate(this.context, R.layout.record_microphone, null);
        mPop = new PopupWindowFactory(this.context, view);

        // PopupWindow布局文件里面的控件
        mImageView = (ImageView) view.findViewById(R.id.iv_recording_icon);
        mTextView = (TextView) view.findViewById(R.id.tv_recording_time);

        mAudioRecoderUtils = new AudioRecoderUtils();

        //录音回调
        mAudioRecoderUtils.setOnAudioStatusUpdateListener(new AudioRecoderUtils.OnAudioStatusUpdateListener() {
            // 录音中....db为声音分贝，time为录音时长
            @Override
            public void onUpdate(double db, long time) {
                mImageView.getDrawable().setLevel((int) (3000 + 6000 * db / 100));
                mTextView.setText(TimeUtils.long2String(time));
                if(listener!=null) listener.onUpdate(db, time);
            }

            // 录音结束，filePath为保存路径
            @Override
            public void onStop(String filePath) {
                mTextView.setText(TimeUtils.long2String(0));
                if(listener!=null) listener.onStop(filePath);
            }
        });
    }
}
